<template>
  <div class="login">
    <div class="login_box">
      <div class="login_avatar_box">
        <img src="../assets/logo.png" />
      </div>
      <!-- Login Form -->
      <el-form
        ref="loginformRef"
        :model="loginform"
        :rules="rules"
        class="login_form"
      >
        <el-form-item prop="username">
          <el-input v-model="loginform.username">
            <i slot="prefix" class="iconfont icon-user"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginform.password" type="password">
            <i slot="prefix" class="iconfont icon-3702mima"></i>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('loginformRef')"
            >立即创建</el-button
          >
          <el-button @click="resetForm('loginformRef')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginform: {
        username: 'admin',
        password: '123456',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 8,
            message: '密码长度在 3 到 8 个字符',
            trigger: 'blur'
          }
        ],
        date1: [
          {
            type: 'date',
            required: true,
            message: '请选择日期',
            trigger: 'change'
          }
        ],
        date2: [
          {
            type: 'date',
            required: true,
            message: '请选择时间',
            trigger: 'change'
          }
        ],
        type: [
          {
            type: 'array',
            required: true,
            message: '请至少选择一个活动性质',
            trigger: 'change'
          }
        ],
        resource: [
          { required: true, message: '请选择活动资源', trigger: 'change' }
        ],
        desc: [{ required: true, message: '请填写活动形式', trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const { data: result } = await this.$http.post(
            '/login',
            this.loginform
          )
          if (result.meta.status !== 200) {
            this.$message.error('错了哦，用户名和密码不匹配')
            return
          }

          console.log(result.data.token)
          window.sessionStorage.setItem('token', result.data.token)
          this.$router.push('/home')
        } else {
          this.$message.error('错了哦，这是一条错误消息')
          return false
        }
      })
    },
    resetForm(formName) {
      // console.log(this)
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang="less" scoped>
.login {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  background-color: white;
  width: 450px;
  height: 300px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .login_avatar_box {
    width: 130px;
    height: 130px;
    border-radius: 50%;
    border: 1px solid #2b4b6b;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    transform: translate(-50%, -50%);
    left: 50%;
    background-color: #fff;
    img {
      background: #eee;
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }
}
.login_form {
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  bottom: 0px;
  position: absolute;
  .btns {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
